package com.dbfmp.dynamic.demo.common.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;

import javax.activation.DataSource;
import java.util.Map;
import java.util.Properties;

/**
 * @NAME: DataSourceProperties
 * @AUTHOR huangtai
 * @DATE: 2019-12-07 18:01
 **/
@Configuration
public class DataSourceProperties {

    @ConfigurationProperties(prefix = "spring.dynamic-datasource.druid-datasources.master")
    @Bean(name = "masterDataSource")
    public DruidDataSource MasterDataSource(StandardEnvironment env){
        DruidDataSource druidDataSource = DruidDataSourceBuilder.create().build();
        return druidDataSource;
    }

    @ConfigurationProperties(prefix = "spring.dynamic-datasource.druid-datasources.slave")
    @Bean(name = "slaveDataSource")
    public DruidDataSource SlaveDataSource(StandardEnvironment env) {
        DruidDataSource druidDataSource = DruidDataSourceBuilder.create().build();
        return druidDataSource;
    }

    //todo 注入连接池属性
    /*public DataSource common(StandardEnvironment env, DruidDataSource druidDataSource){
        Properties properties = new Properties();
        PropertySource<?> appProperties =  env.getPropertySources().get("applicationConfig: [classpath:/application.yml]");
        Map<String,Object> source = (Map<String, Object>) appProperties.getSource();
        properties.putAll(source);
        druidDataSource.configFromPropety(properties);
        return druidDataSource;
    }*/



}
